import { Request, Response} from "express";
import CurriculumService from "@/services/CurriculumService";
import CurriculumModel from "@/models/CurriculumModel";
import {send} from "@/utils/responseUtils";

export default class CurriculumsController {
    /**
     * 更新课程
     * @param req
     * @param res
     */
    public async updateCurriculum(req: Request, res: Response){
        const body=req.body
        console.log("body",body)
        if (!body.id){
            return res.send("课程id不能为空")
        }
        const data=await CurriculumService.update(body.id,body)
        console.log("data",data)
        send(res,{code:200,data})
    }
    /**
     * 获取课程
     * @param req
     * @param res
     */
    public getCurriculum(req: Request, res: Response) {
        console.log('getCurriculum-----req',req.query,req.body,req.params)
        CurriculumService.get({ id:req.query.id as string}).then(Curriculums => {
            res.send(Curriculums)
        }).catch(error => {
            res.send(error)
        })
    }

    /**
     * 添加课程
     * @param request
     * @param response
     */
    public async createCurriculum(request: Request, response: Response) {
        console.log("createCurriculum=================request.body",request.body)
        CurriculumService.create(request.body).then(res => {
            console.log("createCurriculum=================res",res)
            response.send(res)
        })
            .catch(error => {
                console.log("createCurriculum=================error",error)
                response.send(error.message)
            })
    }

    /**
     * 删除课程
     * @param request
     * @param response
     */
    public async deleteCurriculum(request: Request, response: Response){
        console.log("request",request)
        const Curriculum=await CurriculumService.delete(request.body.id)

        send(response,{code:200,message:Curriculum?"删除成功":"删除失败"})
    }

}